//
//  WBStatusPictureView.swift
//  WeiboTen
//
//  Created by ItHeiMa on 2017/7/9.
//  Copyright © 2017年 itHeima. All rights reserved.
//

import UIKit

fileprivate let baseTag = 545

class WBStatusPictureView: UIView {
    
    var statusViewModel: WBStatusViewModel? {
        didSet {
            //先把所有的imageView先隐藏起来
            for i in 0..<9 {
                self.viewWithTag(i+baseTag)?.isHidden = true
            }
            
            //遍历图片的数据，找到imageView,显示并设值（遍历萝卜找篮子 ）
            if let pic_urls = statusViewModel?.picUrls {
                var index = 0
                for pictureModel in pic_urls {
                    //如果有4张图片，就跳过第三个篮子
                    if index == 2 && pic_urls.count == 4 {
                        index += 1
                    }
                    
                    let imageView = viewWithTag(index+baseTag) as! UIImageView
                    imageView.isHidden = false
                    
                    if index == 0 && pic_urls.count == 1 {
                        imageView.frame.size = statusViewModel!.pictureSize
                    } else if index == 0 {
                        imageView.frame.size = homeVS.imageS
                    }
                    
                    imageView.wb_setImage(urlStr: pictureModel.middle_pic, placeHolderImage: "avatar_default_big")
                    
                    index += 1
                }
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension WBStatusPictureView {
    func setupUI () {
        //1. 创建9个小图片视图
        let rect = CGRect(origin: CGPoint.zero, size: homeVS.imageS)
        let gap = homeVS.imageH + 10
        
        for i in 0..<9 {
            let imageView = UIImageView()
            imageView.tag = baseTag + i
            self.addSubview(imageView)
            
            let row = i / 3 //行数
            let col = i % 3 //列数
            let frame = rect.offsetBy(dx: CGFloat(col)*gap, dy: CGFloat(row)*gap)
            imageView.frame = frame
            //scaleAspectFill,和fit都会等比例显示，但是fit只会被限制在容器的内部
            imageView.contentMode = .scaleAspectFill
            imageView.clipsToBounds = true
            imageView.backgroundColor = UIColor.green
            
            //添加点击事件
            imageView.isUserInteractionEnabled = true
            let tap = UITapGestureRecognizer(target: self, action: #selector(clickImage(tap:)))
            imageView.addGestureRecognizer(tap)
        }
        
        self.clipsToBounds = true
    }
}

// MARK: - 事件处理
extension WBStatusPictureView {
    @objc func clickImage(tap: UITapGestureRecognizer) {
        //1. 将当前点击的哪一张图片（index），和当前cell里面的所有图片的url[url]都传出来
        var index = (tap.view?.tag)! - baseTag
        
        if let pic_urls = statusViewModel?.picUrls {
            //2. 如果是4张图片，需要做处理
            if pic_urls.count == 4 && index > 2 {
                index -= 1
            }
            
            //3. 发通知
            let userInfo: [AnyHashable : Any] = ["index": index, "urls": pic_urls]
            let notification = Notification(name: clickStatusImageNotification, object: nil, userInfo: userInfo)
            NotificationCenter.default.post(notification)
        }
    }
}


